package com.ydj.framework.utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.Collection;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

public class ExJsonObject extends JSONObject {

    private static final long LONG_JSON_MAX = 1000000000L;

    public ExJsonObject() {
        super();
    }

    public ExJsonObject(JSONObject jo, String[] names) {
        super(jo, names);
    }

    public ExJsonObject(JSONTokener x) throws JSONException {
        super(x);
    }

    public ExJsonObject(Map<?, ?> m) {
        super(m);
    }

    public ExJsonObject(Object bean) {
        super(bean);
    }

    public ExJsonObject(Object object, String[] names) {
        super(object, names);
    }

    public ExJsonObject(String source) throws JSONException {
        super(source);
    }

    public ExJsonObject(String baseName, Locale locale) throws JSONException {
        super(baseName, locale);
    }

    protected ExJsonObject(int initialCapacity) {
        super(initialCapacity);
    }

    @Override
    public JSONObject put(String key, Object value) throws JSONException {
        if (value == null) {
            return super.put(key, "");
        }

        return super.put(key, value);
    }

    public JSONObject put(String key, Boolean value) throws JSONException {
        return super.put(key, value == null ? false : value);
    }

    public JSONObject put(String key, Double value) throws JSONException {
        return super.put(key, value == null ? "" : String.valueOf(value));
    }

    @Override
    public JSONObject put(String key, double value) throws JSONException {
        return super.put(key, String.valueOf(value));
    }

    public JSONObject put(String key, Float value) throws JSONException {
        return super.put(key, value == null ? "" : String.valueOf(value));
    }

    @Override
    public JSONObject put(String key, float value) throws JSONException {
        return super.put(key, String.valueOf(value));
    }

    public JSONObject put(String key, Integer value) throws JSONException {
        return super.put(key, value == null ? "" : value);
    }

    public JSONObject put(String key, Long value) throws JSONException {
        if (value == null) {
            return super.put(key, "");
        }
        if (value > LONG_JSON_MAX) {
//            return super.put(key, String.valueOf(value));
        }
        return super.put(key, value);
    }

    public JSONObject put(String key, Date value) throws JSONException {
        return super.put(key, value == null ? "" : DateUtils.dateToSecond(value));
    }

    @Override
    public JSONObject put(String key, Collection<?> value) throws JSONException {
        return super.put(key, value == null ? new JSONArray() : value);
    }

    @Override
    public JSONObject put(String key, Map<?, ?> value) throws JSONException {
        return super.put(key, value == null ? new JSONArray() : value);
    }

    @Override
    public JSONObject put(String key, long value) throws JSONException {
        if (value > LONG_JSON_MAX) {
//            return super.put(key, String.valueOf(value));
        }
        return super.put(key, (Object) value);
    }

    /**
     * 强制转换为String
     *
     * @return
     */
    public String getCastString(String key) {
        Object object = this.get(key);
        if (object instanceof String) {
            return (String) object;
        } else if (object instanceof Integer) {
            return String.valueOf((Integer) object);
        } else {
            throw new JSONException("JSONObject[" + quote(key) + "] not a string or not a Int");
        }
    }
}